project(xexec VERSION 1.0.0)

# 设置包版本变量（供 write_basic_package_version_file 使用）
set(XEXEC_VERSION ${PROJECT_VERSION})

add_library(xexec STATIC)

file(GLOB LIBSRC *.cpp)

target_sources(xexec PRIVATE ${LIBSRC})

# 移除对源目录的直接引用，使用下面的 BUILD/INSTALL 接口配置

# 在构建时使用源目录作为 include，在安装时使用 include/xexec
target_include_directories(xexec
  PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    $<INSTALL_INTERFACE:include/xexec>
)

include(CMakePackageConfigHelpers)

# 安装 lib 目标并导出 targets
install(TARGETS xexec
  EXPORT xexecTargets
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include/xexec
)

# 安装头文件（如果有公共头放在 include/ 下）
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
  DESTINATION include/xexec
  FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" PATTERN "*.inl"
)

set(CONFIG_INSTALL_DIR lib/cmake/xexec)

write_basic_package_version_file(
  "${CMAKE_CURRENT_BINARY_DIR}/xexecConfigVersion.cmake"
  VERSION ${XEXEC_VERSION}
  COMPATIBILITY AnyNewerVersion
)

configure_package_config_file(
  "${CMAKE_CURRENT_LIST_DIR}/cmake/xexecConfig.cmake.in"
  "${CMAKE_CURRENT_BINARY_DIR}/xexecConfig.cmake"
  INSTALL_DESTINATION ${CONFIG_INSTALL_DIR}
)

install(FILES
  "${CMAKE_CURRENT_BINARY_DIR}/xexecConfig.cmake"
  "${CMAKE_CURRENT_BINARY_DIR}/xexecConfigVersion.cmake"
  DESTINATION ${CONFIG_INSTALL_DIR}
)

install(EXPORT xexecTargets
  FILE xexecTargets.cmake
  NAMESPACE xexec::
  DESTINATION ${CONFIG_INSTALL_DIR}
)

# 在构建树中导出（可用于未安装时通过 CMAKE_PREFIX_PATH 指向构建目录）
export(EXPORT xexecTargets FILE "${CMAKE_CURRENT_BINARY_DIR}/xexecTargets.cmake")
